package demo.chain.exception;


import cn.hutool.core.exceptions.ValidateException;
import com.lkcoffee.demo.chain.annocation.Length;
import com.lkcoffee.demo.chain.annocation.Max;
import com.lkcoffee.demo.chain.annocation.Min;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : feixiang.li
 * @since : 2025-09-17 17:38
 */
public class ValidatorChain {

    private final List<ValidatorHandler> validatorHandlers = new ArrayList<>();

    private ValidatorContext validatorContext = null;

    public void addLast(ValidatorHandler maxValidatorHandler) {
        validatorHandlers.add(maxValidatorHandler);
    }


    public String getError() {
        return validatorContext.getError();
    }


    public void validate(Object obj) throws ValidateException {
        validatorContext = new ValidatorContext(obj);
        int index = 0;
        while (true) {
            if (index >= validatorHandlers.size()) {
                break;
            }
            ValidatorHandler validatorHandler = validatorHandlers.get(index);
            // 已经被控制了状态
            if (index != validatorContext.getCurrentIndex()) {
                break;
            }
            validatorHandler.validate(obj, validatorContext);
            index++;
        }
    }

}
